package org.bonorsoft.modules.hospital.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.DateUtils;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.hospital.entity.ZzyjsView;
import org.bonorsoft.modules.hospital.service.IZzyjsViewService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.modules.hospital.service.impl.TotalServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: His系统视图
 * @Author: WilliamYe
 * @Date:   2020-11-04
 * @Version: V1.0
 */
@Slf4j
@RestController
@RequestMapping("/hospital/zzyjsView")
public class ZzyjsViewController extends BonorController<ZzyjsView, IZzyjsViewService> {
	@Autowired
	private IZzyjsViewService zzyjsViewService;

	/**
	 * 分页列表查询
	 *
	 * @param zzyjsView
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "His系统视图-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ZzyjsView zzyjsView,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZzyjsView> queryWrapper = QueryGenerator.initQueryWrapper(zzyjsView, req.getParameterMap());
		Page<ZzyjsView> page = new Page<ZzyjsView>(pageNo, pageSize);
		IPage<ZzyjsView> pageList = zzyjsViewService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 今日实时数据
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @AutoLog(value = "His系统视图-今日数据")
	 @GetMapping(value = "/today")
	 public Result<?> queryPageToday(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="16") Integer pageSize) {
		 QueryWrapper<ZzyjsView> queryWrapper = new QueryWrapper<>();
		 Map<String, String> today = DateUtils.getToday();
		 queryWrapper.between("pat_in_time", today.get("start_time"),today.get("end_time"))
				 .orderByDesc("pat_in_time");
		 Page<ZzyjsView> page = new Page<ZzyjsView>(pageNo, pageSize);
		 IPage<ZzyjsView> pageList = zzyjsViewService.page(page, queryWrapper);
		 return Result.ok(pageList);
	 }

	/**
	 * 添加
	 *
	 * @param zzyjsView
	 * @return
	 */
	@AutoLog(value = "His系统视图-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ZzyjsView zzyjsView) {
		zzyjsViewService.save(zzyjsView);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param zzyjsView
	 * @return
	 */
	@AutoLog(value = "His系统视图-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ZzyjsView zzyjsView) {
		zzyjsViewService.updateById(zzyjsView);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "His系统视图-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		zzyjsViewService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "His系统视图-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zzyjsViewService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "His系统视图-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ZzyjsView zzyjsView = zzyjsViewService.getById(id);
		return Result.ok(zzyjsView);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param zzyjsView
   */
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, ZzyjsView zzyjsView) {
      return super.exportXls(request, zzyjsView, ZzyjsView.class, "His系统视图");
  }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, ZzyjsView.class);
  }

}
